/*
 *  Copyright 2010 mazhao.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package shopping.model;

import org.apache.ibatis.session.SqlSession;
import shopping.commons.BaseAction;
import shopping.commons.ModelObject;

import java.util.List;

/**
 * @author mazhao
 */
public class Catalog extends CatalogData {

    /*================================================
      common section for load update delete
     ================================================*/

    public boolean save() {
        String sqkKeyUpdateLft = getClassName() + ".updateCatalogLftWhenInsert";
        String sqlKeyUpdateRgt = getClassName() + ".updateCatalogRgtWhenInsert";
        String sqlKeySave = getClassName() + "." + getCallerMethodName();
        log.debug("sql key " + sqlKeySave);

        if (autoTrans) {
            session = getSession();
        }
        session.update(sqkKeyUpdateLft, this);
        session.update(sqlKeyUpdateRgt, this);
        int cnt = session.insert(sqlKeySave, this);
        log.debug("insert catalog " + cnt);
        if (autoTrans) {
            session.commit();
            session.close();
        }
        return cnt == 1;
    }

    /**
     * Delete current catalog.
     * 1. delete catalog
     * 2. upate other lft and rgt
     *
     * @return
     */
    public boolean delete() {
        String sqlKeyDelete = getClassName() + "." + getCallerMethodName();
        String sqlKeyRgtUpdate = getClassName() + ".updateCatalogRgtWhenDelete";
        String sqlKeyLftUpdate = getClassName() + ".updateCatalogLftWhenDelete";

        log.debug("sql key for delete " + sqlKeyDelete);
        log.debug("sql key for update rgt " + sqlKeyRgtUpdate);
        log.debug("sql key for update lft " + sqlKeyLftUpdate);

        if (autoTrans) {
            session = getSession();
        }

        session.delete(sqlKeyDelete, this);
        session.update(sqlKeyRgtUpdate, this);
        session.update(sqlKeyLftUpdate, this);

        log.debug("delete catalog successfully, the catalog is" + this);

        if (autoTrans) {
            session.commit();
            session.close();
        }
        return true;
    }

    /*================================================
      specific logic section
     ================================================*/


    public static List<Catalog> selectAllCatalog() {
        String sqlKey = Catalog.class.getName() + ".selectAllCatalog" ;
        staticLog.debug("sql key " + sqlKey);

        SqlSession session = getSession();

        List<Catalog> catalogs = session.selectList(sqlKey);
        staticLog.debug("find catalog " + catalogs);
        session.close();

        return catalogs;
    }

    /**
     * get all catalog list.
     * CatalogAction.list -> Catalog.selectCatalogList
     *
     * @return
     */
    public List<Catalog> selectCatalog() {
        String sqlKey = getClassName() + "." + getCallerMethodName();
        log.debug("sql key " + sqlKey);

        if (autoTrans) {
            session = getSession();
        }

        List<Catalog> catalogs = session.selectList(sqlKey);
        log.debug("find catalog " + catalogs);
        if (log.isDebugEnabled()) {
            for (Catalog c : catalogs) {
                log.debug(c);
            }
        }

        if (autoTrans) {
            session.close();
        }
        return catalogs;
    }

    public List<Catalog> pathToMe() {
        String sqlKey = getClassName() + "." + getCallerMethodName();
        log.debug("sql key " + sqlKey);

        if (autoTrans) {
            session = getSession();
        }

        List<Catalog> catalogs = session.selectList(sqlKey, this);
        log.debug("find catalog " + catalogs);
        if (autoTrans) {
            session.close();
        }
        return catalogs;
    }

    public List<Catalog> subCatalogs() {
        String sqlKey = getClassName() + "." + getCallerMethodName();
        log.debug("sql key " + sqlKey);

        if (autoTrans) {
            session = getSession();
        }

        List<Catalog> catalogs = session.selectList(sqlKey, this);
        log.debug("find catalog " + catalogs);
        if (autoTrans) {
            session.close();
        }
        return catalogs;
    }

}
